﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class FundsOperatingController : Controller
    {
        private FundsOperatingInfo funinfo = new FundsOperatingInfo(true);
        public ActionResult FundsOperatingIndex()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["GroupDept"] = GetGroupDept("-1");
            return View();
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDept(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetdeptName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取增款名称列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDepositTypeIDIncrease()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            DataSet ds = funinfo.GetDepositTypeIDIncrease();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(ds.Tables[0].Rows[i]["DepositName"].ToString().Trim(), ds.Tables[0].Rows[i]["DepositName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取减款名称列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDepositTypeIDReduce()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            DataSet ds = funinfo.GetDepositTypeIDReduce();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(ds.Tables[0].Rows[i]["DepositName"].ToString().Trim(), ds.Tables[0].Rows[i]["DepositName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取已发卡且状态为正常的卡的列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetFundsOperatingList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                string strDep_ID = "";
                string strGroup_ID = "";
                string strCardSerial = " and 1=1 ";
                if (param[2] != "-1")
                {
                    strDep_ID += " and Dep_ID='" + param[2] + "'";
                }
                else
                {
                    strDep_ID += " and 1=1 ";
                }
                if (param[4] != "-1")
                {
                    strGroup_ID += " and GroupIdDept='" + param[4] + "'";
                }
                else
                {
                    strGroup_ID += " and 1=1";
                }
                if (param[3] != "")
                {
                    strCardSerial = " and CardSerial=" + param[3];
                }
                query = " and EmpNo like '%" + param[0] + "%' and PName like '%" + param[1] + "%'" + strDep_ID + strCardSerial + strGroup_ID;
            }
            IList<FundsOperatingInfo> funlist = funinfo.SelFundsOperatingInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = funinfo.GetFundsOperatingCount(query);
            List<string> v;
            foreach (FundsOperatingInfo item in funlist)
            {
                #region
                v = new List<string>();
                v.Add(item.CardNo.ToString());
                v.Add(item.EmpNo);
                v.Add(item.PName);
                v.Add(item.CardSerial.ToString());
                v.Add(item.deptName);
                v.Add(item.PersonTypeName);
                v.Add(item.Balance.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.CardNo.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 点击增款时执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult IncreaseMoney(string CardNo)
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["cardleft"] = SelLeftBox("");
            ViewData["cardright"] = SelRightBox();
            ViewData["DepositTypeIDIncrease"] = GetDepositTypeIDIncrease();
            //string strCardNo = CardNo.Remove(0, 1);
            //FundsOperatingVM vm = new FundsOperatingVM();
            //vm.CardNoAdd = strCardNo;
            //return View(vm);
            return View();
        }
        /// <summary>
        /// 点击减款时执行的方法
        /// </summary>
        /// <param name="CardNo"></param>
        /// <returns></returns>
        public ActionResult ReduceMoney(string CardNo)
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["cardleft"] = SelLeftBox("");
            ViewData["cardright"] = SelRightBox();
            ViewData["DepositTypeIDReduce"] = GetDepositTypeIDReduce();
            //string strCardNo = CardNo.Remove(0, 1);
            //FundsOperatingVM vm = new FundsOperatingVM();
            //vm.Flag = "Reduce";
            //vm.CardNoAdd = strCardNo;
            return View();
        }

        /// <summary>
        /// 保存（给卡增款）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveIncreaseMoney(FundsOperatingVM model)
        {
            string empall = model.EmpAll.Remove(model.EmpAll.Length - 1);
            string[] sempall = empall.Split(',');
            int result = 0;
            int countNo = 0;
            if (ModelState.IsValid)
            {
                FundsOperatingInfo fun = new FundsOperatingInfo()
                {
                    DepositName = model.DepositName,
                    Money = Convert.ToDecimal(model.Money) * 100,
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName
                };
                result = funinfo.SaveIncreaseMoney(fun, sempall, out countNo);
            }
            return Json(result);
        }
        /// <summary>
        /// 保存（给卡减款）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveReduceMoney(FundsOperatingVM model)
        {
            string empall = model.EmpAll.Remove(model.EmpAll.Length - 1);
            string[] sempall = empall.Split(',');
            int result = 0;
            if (ModelState.IsValid)
            {
                FundsOperatingInfo fun = new FundsOperatingInfo()
                {
                    DepositName = model.DepositName,
                    Money = Convert.ToDecimal(model.Money) * 100,
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName
                };
                result = funinfo.SaveReduceMoney(fun, sempall);
            }
            return Json(result);
        }

        public ActionResult CheckMoney(string Flag, string Money, string CardNoAdd)
        {
            bool istongguo = true;
            decimal dMoney = Convert.ToDecimal(Money);
            if (Flag == "Reduce")
            {
                istongguo = funinfo.CheckMoney(dMoney, CardNoAdd);
            }
            return Json(istongguo);
        }

        public ActionResult GetLeftBox(string Select)
        {
            ViewData["cardleft"] = SelLeftBox(Select);
            return Json(ViewData["cardleft"]);
        }

        public IList<SelectListItem> SelRightBox()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取下拉列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> SelLeftBox(string Select)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(Select))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = Select.Split(',');
                string strDep_ID = "";
                string strCardSerial = " and 1=1 ";
                if (param[2] != "-1")
                {
                    strDep_ID = " and Dep_ID='" + param[2] + "'";
                }
                else
                {
                    strDep_ID += " and 1=1 ";
                }
                if (param[3] != "")
                {
                    strCardSerial = " and CardSerial=" + param[3];
                }
                query = " and EmpNo like '%" + param[0] + "%' and PName like '%" + param[1] + "%'" + strDep_ID + strCardSerial;
            }
            IList<FundsOperatingInfo> funlist = funinfo.SelLeftBox(query);
            IDictionary<string, string> dic = new Dictionary<string, string>();
            //dic.Add(1, "张三");
            //dic.Add(2, "李四");
            //dic.Add(3, "王五");
            //dic.Add(4, "朱六");
            foreach (FundsOperatingInfo item in funlist)
            {
                dic.Add(item.EmpNo, item.PName);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
    }
}
